package com.smartlifev30.baiwei.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.smartlifev30.Constants;
import com.smartlifev30.R;
import com.smartlifev30.baiwei.adapter.GatewayAdapter;
import com.smartlifev30.baiwei.bean.Gateway;
import com.smartlifev30.baiwei.view.recyclerViewComponent.BaseAdapter;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.core.ItemTouchHelperCallback;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.core.ItemTouchHelperExtension;
import com.smartlifev30.mvp.module.GatewayActivityModule;
import com.smartlifev30.mvp.presenter.GatewayActivityPresent;
import com.smartlifev30.mvp.view.IGatewayActivityView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import smartlifev30.com.sdk.bean.response.GatewayInfo;

/**
 * LoginActivity(手动点击登录)   ==> 1.正常登录成功  2.wifi下登录超时
 * PrepareActivity(后台根据记录登录) ==> 1.正常登录成功  2.wifi下登录超时
 *
 * @author tys
 */
public class GatewayActivity extends BaseActivity<GatewayActivityPresent> implements IGatewayActivityView {

    @BindView(R.id.gw_recycler_view)
    RecyclerView recyclerView;
    private GatewayAdapter adapter;

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            GatewayInfo obj = (GatewayInfo) msg.obj;
            adapter.addDataAndNotify(obj);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gateway);
        ButterKnife.bind(this);
        initView();
        initData();
    }


    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ItemTouchHelperExtension itemTouchHelper = new ItemTouchHelperExtension(new ItemTouchHelperCallback());
        adapter = new GatewayAdapter(this, new ArrayList<>(), itemTouchHelper);
        recyclerView.setAdapter(adapter);
        itemTouchHelper.attachToRecyclerView(recyclerView);
        adapter.setMenuClickListen((view, position) -> {

            Toast.makeText(this, position + " item", Toast.LENGTH_SHORT).show();
        }).setItemClickListen((itemView, position) -> {
            presenter.entryTargetGateway(GatewayActivity.this, adapter.getData(position));
        });
    }

    private void initData() {
        Intent intent = getIntent();
        String arg1 = intent.getStringExtra("arg1");
        Log.i("0-0-", "进入网关界面方式:" + arg1);
        int arg2 = intent.getIntExtra("arg2", -1);
        if (arg2 == Constants.LOGIN_TIME_OUT) {

        }
    }

    @Override
    protected GatewayActivityPresent createPresent() {
        return new GatewayActivityPresent(this, new GatewayActivityModule());
    }

    public void titleBack(View view) {
        onBackPressed();
    }

    @Override
    public void addGateway(GatewayInfo gateways) {
        List<GatewayInfo> data = adapter.getData();
        if (!data.contains(gateways)) {
            Message message = handler.obtainMessage(0, gateways);
            handler.sendMessage(message);
        }
    }

    @Override
    public void toMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.searchGateway();
    }

    @Override
    protected void onPause() {
        super.onPause();
        presenter.stopSearchGateway();
    }
}
